package org.infosys.org.service.impl;

import org.infosys.basic.service.impl.BaseServiceImpl;
import org.infosys.org.domain.Department;
import org.infosys.org.mapper.DepartmentMapper;
import org.infosys.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    /**
    * @Title: save
    * @Description: 新增一个部门
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/21 17:43
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        String dirPath= "";
        if(ObjectUtils.isEmpty(department.getParent())) {
            dirPath = "/" + department.getId();
        } else {
            Department pardentDept = departmentMapper.loadById(department.getParent().getId());
            if(!ObjectUtils.isEmpty(pardentDept)) {
                dirPath = pardentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
    * @Title: update
    * @Description: 修改部门信息
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/21 17:43
    * @Parameters: [department]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Department department) {
        String dirPath= "";
        if(ObjectUtils.isEmpty(department.getParent())) {
            dirPath = "/" + department.getId();
        } else {
            Department pardentDept = departmentMapper.loadById(department.getParent().getId());
            if(!ObjectUtils.isEmpty(pardentDept)) {
                dirPath = pardentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    /**
    * @Title: remove
    * @Description: 根据id删除一个部门
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/21 17:43
    * @Parameters: [id]
    * @Return void
    */
    @Override
    @Transactional
    public void remove(Long id) {
         // 父级部门为空
        departmentMapper.remove(id);


    }

    /**
    * @Title: departmentTree
    * @Description: 部门级联查询
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/26 13:18
    * @Parameters: []
    * @Return void
    */
    @Override
    public List<Department> departmentTree() {
        List<Department> depart = departmentMapper.loadAll();
        Map<Long, Department> hashmap = new HashMap<Long, Department>();

        // Map<Long, Department> deptMap = deptTree.stream().collect(Collectors.toMap(Department::getId, item -> item));
        for (Department department:depart) {
        hashmap.put(department.getId(),department);
        }
        ArrayList<Department> departments = new ArrayList<>();

        for (Department department:depart) {
            // 没有父级部门ID就是顶级标签，直接放入到list集合中
            if (ObjectUtils.isEmpty(department.getParentId())) {
                departments.add(department);
            } else {// 有则放入到父级部门的child字段中
                // 根据Parent_id获取到父级部门对象
                Department parent = hashmap.get(department.getParentId());
                // 能查询到父级对象就说明有父级部门，放入到父级部门的child字段中
                if (!ObjectUtils.isEmpty(parent)) {
                    parent.getChildren().add(department);
                }
            }
        }
        return departments;
    }

}
